package com.github.tetrisgame.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.tetrisgame.manager.BlockManager;
import com.github.tetrisgame.manager.GameCenter;
import com.github.tetrisgame.model.Block;
import com.github.tetrisgame.model.GridModel;
import com.github.tetrisgame.utils.Constants;

/**
 * 显示游戏界面
 */
public class GameView extends View {
    private int row = Constants.ROW;
    private int column = Constants.COLUMN;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int leftMargin = 0;
    private int topMargin = 0;

    public GameView(Context context) {
        this(context,null);
    }

    public GameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //如果父容器没有指定具体尺寸的情况下

        //处理之后得到真实的尺寸
        int realWidth ;
        int realHeight;
        //确定真实的宽度
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY){
            realWidth = widthSize;
        }else{
            realWidth = Constants.COLUMN * Constants.SIZE;
        }

        //确定真实的高度
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY){
            realHeight = heightSize;
        }else{
            realHeight = Constants.ROW * Constants.SIZE;
        }

        //确定一下在这个尺寸下对应的行和列是多少
        row = realHeight / Constants.SIZE;
        column = realWidth / Constants.SIZE;

        //给真实的尺寸
        setMeasuredDimension(realWidth,realHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //计算左间距（确保绘制区域在中心位置
        leftMargin = (getWidth() - column * Constants.SIZE)/2;
        topMargin = (getHeight() - row * Constants.SIZE)/2;

        //将当前测量之后的行和列设置给GameCenter
        GameCenter.defaultCenter.setBoardSize(row,column);
    }

    //刷新界面
    public void refresh(){
        invalidate();
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        //绘制背景方块
        drawBackgroundGrid(canvas);

        //绘制当前图形
        drawCurrentBlock(canvas);

        //绘制面板中固定的方块
        drawFixedBlock(canvas);
    }

    //绘制面板中固定的方块
    private void drawFixedBlock(Canvas canvas) {
        GridModel[][] boards = GameCenter.defaultCenter.boards;
        //创建方块对应的矩形区域对象
        Rect rect = new Rect();
        for (int i = 0; i < boards.length; i++) {
            for (int j = 0; j < boards[0].length; j++) {
                GridModel model = boards[i][j];
                if (model != null && model.hasData()){
                    rect.left = j*Constants.SIZE + leftMargin;
                    rect.top =  i*Constants.SIZE + topMargin;
                    rect.right = rect.left + Constants.SIZE;
                    rect.bottom = rect.top + Constants.SIZE;
                    canvas.drawBitmap(model.getBitmap(),null,rect,null);
                }
            }
        }
    }

    //绘制当前图形
    private void drawCurrentBlock(Canvas canvas){
        //获取当前方块对象
        Block block = BlockManager.defaultManager.getCurrentBlock();
        //获取方块对应的二维数组
        int[][] shapes = block.getShape();
        //获取方块对应的图片资源位图
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),block.imageResource);
        //创建方块对应的矩形区域对象
        Rect rect = new Rect();
        //遍历二维数组，绘制小方块图片
        for (int i = 0; i < shapes.length; i++) {
            for (int j = 0; j < shapes[0].length; j++) {
                if (shapes[i][j] == 1){
                    rect.left = block.column*Constants.SIZE+ j*Constants.SIZE + leftMargin;
                    rect.top = block.row*Constants.SIZE+ i*Constants.SIZE + topMargin;
                    rect.right = rect.left + Constants.SIZE;
                    rect.bottom = rect.top + Constants.SIZE;
                    canvas.drawBitmap(bitmap,null,rect,null);
                }
            }
        }
    }

    //绘制背景方格
    private void drawBackgroundGrid(Canvas canvas){
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                //配置画笔的颜色
                if ((i+j)%2 == 0){
                    mPaint.setColor(Color.parseColor("#2A425B"));
                }else{
                    mPaint.setColor(Color.parseColor("#334C64"));
                }
                canvas.drawRect(j*Constants.SIZE+leftMargin,i*Constants.SIZE+topMargin,(j+1)*Constants.SIZE+leftMargin,(i+1)*Constants.SIZE+topMargin,mPaint);
            }
        }
    }
}











